    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import ch.zarzu.champions.builder.logic.*;
import ch.zarzu.util.*;

public class Advantage extends PowerListItem implements MouseListener {
    private JButton button, lock;
    private String app_path, id;
    private TitlePhrase name, cost;
    private SpringLayout layout;
    private Listener listener;

    public Advantage(String id_string) {
        listener = Listener.getInstance();
        
        app_path = AppPath.get();
        id = id_string;
        
        layout = new SpringLayout();
        setLayout(layout);
        
        name = new TitlePhrase("");
        cost = new TitlePhrase("");
        redraw();

        setUsed(false);
    }
    
    //draw all elements
    private void redraw() {
        removeAll();
        
        //lock button
        ImageIcon icon = new ImageIcon(app_path + "images/misc/lock.png");
        lock = new JButton(icon);
        lock.setBorder(null);
        lock.setFocusable(false);
        lock.setContentAreaFilled(false);
        lock.addMouseListener(this);
        lock.setPreferredSize(new Dimension(icon.getIconWidth() + 2, icon.getIconHeight()));
        lock.setVisible(false);
        
        //background
        icon = new ImageIcon(app_path + "images/advantages/empty_a.png");
        button = new JButton(icon);
        button.setBorder(null);
        button.setFocusable(false);
        button.setContentAreaFilled(false);
        button.setPreferredSize(new Dimension(icon.getIconWidth() + 2, icon.getIconHeight() + 4));
        button.addMouseListener(this);
        
        add(lock);
        add(name);
        add(cost);
        add(button);
        
        layout.putConstraint(SpringLayout.EAST, lock, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, cost, 9, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, cost, -30, SpringLayout.EAST, button);
        layout.putConstraint(SpringLayout.NORTH, name, 9, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, name, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, button);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, button);
    }
    
    public void setName(String n) {
        name = new TitlePhrase(n);
        redraw();
    }
    
    public void setCost(String c) {
        cost = new TitlePhrase(c);
        redraw();
    }
    
    //returns the id
    public String getId() {
        return id;
    }
    
    public void mouseEntered(MouseEvent e) {
        listener.powerMouseOver(id);
    }

    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1 && e.getSource() != lock) {
            listener.advantageClick(id);
        } else if(e.getButton() == MouseEvent.BUTTON3 || e.getSource() == lock) {
            listener.powerLock(id);
        }
    }

    //sets the visibility of the lock
    public void setLockVisible(boolean b) {
        lock.setVisible(b);
    }
    
    public void setUsed(boolean used) {
        if(used) {
            button.setIcon(new ImageIcon(app_path + "images/advantages/empty_b.png"));
        } else {
            button.setIcon(new ImageIcon(app_path + "images/advantages/empty_a.png"));
        }
    }
    
    public void setUnavailable() {
        button.setIcon(new ImageIcon(app_path + "images/advantages/empty_c.png"));
    }
    
    public void setAdded() {
        button.setIcon(new ImageIcon(app_path + "images/advantages/empty_d.png"));
    }
}